//
//  HWTabBar.m
//  01-黑马微博
//
//  Created by lanou3g on 15/9/9.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "HWTabBar.h"

@interface HWTabBar ()
@property (nonatomic, weak)UIButton *plusBtn;
@end
@implementation HWTabBar

- (instancetype)initWithFrame:(CGRect)frame{
   self = [super initWithFrame:frame];
    if (self) {
        UIButton *button = [[UIButton alloc] init];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        button.size = button.currentBackgroundImage.size;
                [self addSubview:button];
        [button addTarget:self action:@selector(plusClick:) forControlEvents:UIControlEventTouchUpInside];
        self.plusBtn = button;
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
//    设置加号按钮的位置
    self.plusBtn.centerX = self.width * 0.5;
    self.plusBtn.centerY = self.height * 0.5;
    CGFloat tabbarButtonW = self.width / 5;
    CGFloat tabbarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            child.width = tabbarButtonW;
            child.x = tabbarButtonIndex * tabbarButtonW;
            //            child.backgroundColor = [UIColor redColor];
            tabbarButtonIndex ++;
            if (tabbarButtonIndex == 2 ) {
                tabbarButtonIndex ++;
            }
        }

    }
//    设置其他tabBarButton的位置和尺寸
//    NSInteger count = self.subviews.count;
//    HWLog(@"%@", self.subviews);
//    for (int i = 0; i < count; i++) {
//        UIView *child = self.subviews[i];
//        Class class = NSClassFromString(@"UITabBarButton");
//        if ([child isKindOfClass:class]) {
//            child.width = tabbarButtonW;
//            child.x = tabbarButtonIndex * tabbarButtonW;
////            child.backgroundColor = [UIColor redColor];
//            tabbarButtonIndex ++;
//            if (tabbarButtonIndex == 2 ) {
//                tabbarButtonIndex ++;
//            }
//        }
//        
//    }
}
- (void)plusClick:(UIButton *)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(tabBarDidClickPlusBtn:)]) {
        [self.delegate tabBarDidClickPlusBtn:sender];
    }
}
@end
